package com.accounting.client.frontend;

import com.accounting.client.ApplicationContext;
import com.accounting.client.frontend.system.CalculateHandler;
import com.accounting.client.frontend.system.SettingsHandler;
import com.accounting.client.frontend.system.StatisticHandler;
import com.accounting.client.resource.BoxResources;
import com.accounting.client.session.Session;
import com.accounting.client.session.SessionManager;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.GwtUtil;
import corelib.client.resource.CommonResources;
import corelib.client.ui.LazyWidget;
import corelib.client.ui.widget.WidgetUtil;
import corelib.client.ui.widget.dialog.SimpleDialog;
import corelib.client.ui.widget.dialog.SimpleLargeDialog;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.toolbar.SimpleMenuBar;
import corelib.client.ui.widget.toolbar.SimpleMenuItem;

public class Header extends LazyWidget<Panel> {
	
	private FlowPanel panel;
	private SimpleMenuBar menuBar;
	
	public Header(){
		panel = new FlowPanel();
		panel.addStyleName(BoxResources.INSTANCE.css().header());
	}
	
	@Override
	protected Panel createWidget() {
		DivElement myAcctElem = Document.get().createDivElement();
		myAcctElem.addClassName(BoxResources.INSTANCE.css().myAccountButton());
		myAcctElem.appendChild(AbstractImagePrototype.create(CommonResources.INSTANCE.user36()).createElement());
		myAcctElem.appendChild(AbstractImagePrototype.create(CommonResources.INSTANCE.arrow()).createElement());
		final Widget myAcctButton = WidgetUtil.createWidget(myAcctElem);
		myAcctButton.addDomHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (menuBar == null) {
					menuBar = createSimpleMenuBar();
				}
				if (menuBar != null) {
					menuBar.showPopup(myAcctButton);
				}
			}
		}, 
			ClickEvent.getType()
		);
		
		SimpleLabel welcome = new SimpleLabel("欢迎 " + Session.Current.getFirstName()).setBold(true);
		welcome.getElement().getStyle().setPadding(15, Unit.PX);
		welcome.getElement().getStyle().setFontSize(15, Unit.PX);
		welcome.getElement().getStyle().setFloat(Style.Float.LEFT);
		welcome.getElement().getStyle().setWidth(30, Unit.PCT);
		
		SimpleLabel currencyField = new SimpleLabel("当前实时汇率: " + ApplicationContext.CurrencyRate + " 数据来源： 欧洲央行")
		.setBold(true)
				;
		currencyField.getElement().getStyle().setColor("red");
		currencyField.getElement().getStyle().setPadding(15, Unit.PX);
		currencyField.getElement().getStyle().setFontSize(15, Unit.PX);
		currencyField.getElement().getStyle().setFloat(Style.Float.LEFT);
		currencyField.getElement().getStyle().setWidth(30, Unit.PCT);
		
		panel.add(welcome);
		panel.add(currencyField);
		panel.add(myAcctButton);
		return this.panel;
	}

	private SimpleMenuBar createSimpleMenuBar() {
		menuBar = new SimpleMenuBar(true);
		SimpleMenuItem calculateBtn = new SimpleMenuItem("计算账务明细",
			CommonResources.INSTANCE.calc32(), new ScheduledCommand(){
				@Override
				public void execute() {
					SimpleDialog.show(new CalculateHandler());
				}
			}
		);
		SimpleMenuItem statisticBtn = new SimpleMenuItem("详情统计",
				BoxResources.INSTANCE.budget32(), new ScheduledCommand(){
					@Override
					public void execute() {
						SimpleLargeDialog.show(new StatisticHandler());
					}
				}
		);
		SimpleMenuItem settingsBtn = new SimpleMenuItem("系统设置",
				BoxResources.INSTANCE.settings32(), new ScheduledCommand(){
					@Override
					public void execute() {
						SimpleDialog.show(new SettingsHandler());
					}
				}
		);
		
		SimpleMenuItem logOff = new SimpleMenuItem("退出",
			CommonResources.INSTANCE.logoff32(), new ScheduledCommand() {
				@Override
				public void execute() {
					SessionManager.logout();
					GwtUtil.reloadPage(true);
				}
			}
	    );
		menuBar.addItem(calculateBtn);
		menuBar.addItem(statisticBtn);
		menuBar.addItem(settingsBtn);
		menuBar.addSeparator();
		menuBar.addItem(logOff);
		return menuBar;
	}

}
